package com.qianfeng.es.service.impl;

//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼                  BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  
//  


import com.qianfeng.es.service.AggregateService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.range.ParsedRange;
import org.elasticsearch.search.aggregations.metrics.ParsedCardinality;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * Created by Jackiechan on 2022/9/8 21:49
 *
 * @author Jackiechan
 * @version 1.0
 * @since 1.0
 */
@Service
public class AggregateServiceImpl implements AggregateService {

    private RestHighLevelClient restHighLevelClient;

    @Autowired
    public void setRestHighLevelClient(RestHighLevelClient restHighLevelClient) {
        this.restHighLevelClient = restHighLevelClient;
    }
    @Override
    public void cardinalityAgg(String index, String filed, String bieming) throws IOException {

        SearchResponse response =   baseAgg(index, new SearchSourceBuilder().aggregation(AggregationBuilders.cardinality(bieming).field(filed)));
        //转成对应的类型,通过debug可以看到具体的类型
        ParsedCardinality aggregation = response.getAggregations().get(bieming);
        System.err.println(aggregation.getValue());
    }

    @Override
    public void range(String index, String filed,String bieming) throws Exception {
        SearchResponse response = baseAgg(index, new SearchSourceBuilder().aggregation(
                AggregationBuilders
                        .range(bieming).field(filed)
                        .addUnboundedTo(5).addUnboundedFrom(10).addRange(5, 10)));
        ParsedRange aggregation = response.getAggregations().get(bieming);
        aggregation.getBuckets().forEach(bucket -> {
            System.err.println(bucket.getKey()+"====>"+bucket.getDocCount());
        });
    }


    public SearchResponse baseAgg(String index,SearchSourceBuilder searchSourceBuilder) throws IOException {
        SearchRequest request = new SearchRequest(index).source(searchSourceBuilder);
        SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
        return response;
    }
}
